<?php
    class Admincp_FurniturecategoryController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_validate;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Furniturecategory();
            $this->_validate = new Admincp_Form_ValidateFashioncategory();
            $this->view->headTitle('Quản lý danh mục đồ gia dụng');
        }
        
        public function preDispatch()
        {
            $auth = Zend_Auth::getInstance();
            if(!$auth->hasIdentity()) {
                if($this->_request->getActionName() != 'login')
                    $this->_redirect('admincp/index/login');
            }
        }
        
        public function indexAction()
        {
            $result = $this->_model->listItem(0, 'all', 0);
			$this->view->total = count($result);
			$this->view->data = $result;

            if($_POST['add']) {
                $this->_redirect('admincp/furniturecategory/create');
            }
            if($_POST['orders']) {
				$result = $this->_model->listCategory();
				$data = array();
				foreach ($result as $row) {
					$data[] = $row;
				}
				$orderArr = $this->_arrParam['ordering'];
				$this->_model->orderTree($data, $orderArr);
                $this->_redirect('admincp/furniturecategory');
			}
        }
        
        public function createAction()
        {
            $result = $this->_model->listCategory();
			$this->view->data = $result;
            
            if($this->_request->isPost()) {
                $name = trim(strip_tags($this->_arrParam['name']));
                $parents = $this->_arrParam['parents'];
                
                if(!$this->_validate->validName($name)) {
                    $this->view->errorName = 'Tên danh mục từ 1-200 kí tự';
                }
                else {
                    $strFlex = new Zendvn_Filter_RemoveCircumflex();
                    $data = array(
                        'name' => $name,
                        'url' => strtolower($strFlex->filter2($name)),
                        'status' => 1,
                    );
                    $this->_model->insertNode($data, $parents, array('position' => 'right'));
                    $this->_redirect('admincp/furniturecategory');
                }
            }
        }
        
        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
			$getCat = $this->_model->getNodeInfo($id);
			$this->view->data2 = $getCat;
			
			$listCat = $this->_model->listCategory();
			$this->view->data = $listCat;
            
            if($this->_request->isPost()) {
                $name = trim(strip_tags($this->_arrParam['name']));
                $parents = $this->_arrParam['parents'];
                
                if(!$this->_validate->validName($name)) {
                    $this->view->errorName = 'Tên danh mục từ 1-200 kí tự';
                }
                else {
                    $strFlex = new Zendvn_Filter_RemoveCircumflex();
                    $data = array(
                        'name' => $name,
                        'url' => strtolower($strFlex->filter2($name)),
                        'status' => 1,
                    );
                    $this->_model->updateNode($data, $id, $parents);
                    $this->_redirect('admincp/furniturecategory');
                }
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->_model->removeNode($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
		    $status = (int) $this->_arrParam['status'];
		    $status = ($status == 1) ? 2 : 1;
		    $data = array(
                'id' => $id,
		        'status' => $status,
		    );
		    $this->_model->saveItem($data, 'update');
		    $this->_redirect($_SERVER['HTTP_REFERER']);
        }
    }
